﻿using System.Data;
using System.Configuration;
using System.Web;
using System.Management;
using System.Text;
using System.IO;
using System.Collections;
using System.Xml;
using System;
using System.Timers;
using System.Xml.Linq;
using System.Collections.Generic;

namespace ATM_Pro.Public
{
    public class FileHandle
    {
        public static readonly string xmlPath = "SetVal.xml";

        #region 构造函数
        public FileHandle()
        {

        }
        #endregion

        #region 删除文件
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="Path">物理路径</param>
        public static void FileDel(string Path)
        {
            File.Delete(Path);
        }
        #endregion

        #region 递归删除文件夹目录及文件
        /// <summary>
        /// 递归删除文件夹目录及文件
        /// </summary>
        /// <param name="dir"></param>  
        /// <returns></returns>
        public static void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir)) //如果存在这个文件夹删除之 
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                        File.Delete(d); //直接删除其中的文件                        
                    else
                        DeleteFolder(d); //递归删除子文件夹 
                }
                Directory.Delete(dir, true); //删除已空文件夹                
            }
        }
        #endregion

        //取得disk大小
        public static UInt64 DiskSize(string path, string propertys)
        {
            ManagementObject size = new ManagementObject("win32_logicaldisk.deviceid=" + path);
            size.Get();
            UInt64 b = 1024;
            UInt64 a = (Convert.ToUInt64(size[propertys]) / b) / b;
            return a;
        }

        //获得磁盘剩余空间大小  'D:' || "D:"
        public static UInt64 GetDiskFreeSpaceSize(string disk)
        {
            WqlObjectQuery woq = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = " + disk + "");
            ManagementObjectSearcher mos = new ManagementObjectSearcher(woq);
            UInt64 a = 0;
            UInt64 b = 1024;
            foreach (ManagementObject mo in mos.Get())
            {
                //Console.WriteLine("Description: " + mo["Description"]);
                //Console.WriteLine("File system: " + mo["FileSystem"]);
                //Console.WriteLine("Free disk space: " + mo["FreeSpace"]);
                //Console.WriteLine("Size: " + mo["Size"]);
                a += (Convert.ToUInt64(mo["FreeSpace"]) / b) / b;

            }
            return a;
        }

        public static string dbName = "DB_Pic.mdb";
        public static string GeneratorDBConnectionString(string path)
        {
            return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=admin;Password=;", Path.Combine(path, dbName));
        }

        #region 获得指定文件夹的大小 -- 不用
        public static long GetSize(string path)
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
            long count = 0;
            foreach (System.IO.FileSystemInfo fi in dir.GetFileSystemInfos())
            {
                if (fi.Attributes.ToString().ToLower().Equals("directory"))
                {
                    count += GetSize(fi.FullName);
                }
                else
                {
                    System.IO.FileInfo finf = new System.IO.FileInfo(fi.FullName);
                    count += finf.Length;
                }
            }
            return count;
        }  //录像一天4320MB 约 4.2GB 一周 30GB

        public static long GetFileSize(string path)
        {
            long fileCount = 0;
            if (Directory.Exists(path))
            {
                string[] filePath = Directory.GetDirectories(path);
                foreach (string file in filePath)
                {
                    fileCount += GetSize(file);
                }
            }
            return fileCount;
        }
        #endregion
    }


}
//using System;
//using System.Threading;
//using System.Collections;  
//using System.Collections.Generic;

//namespace temp
//{
//    public class SyncEvents
//    {
//        public SyncEvents()
//        {
//            _newItemEvent = new AutoResetEvent(false);
//            _exitThreadEvent = new ManualResetEvent(false);
//            _eventArray = new WaitHandle[2];
//            _eventArray[0] = _newItemEvent;
//            _eventArray[1] = _exitThreadEvent;
//        }
//        public EventWaitHandle ExitThreadEvent { get { return _exitThreadEvent; } }
//        public EventWaitHandle NewItemEvent { get { return _newItemEvent; } }
//        public WaitHandle[] EventArray { get { return _eventArray; } }

//        private EventWaitHandle _newItemEvent;
//        private EventWaitHandle _exitThreadEvent;
//        private WaitHandle[] _eventArray;
//    }


//    public class Producer
//    {
//        public Producer(Queue<int> q, SyncEvents e) 
//        {
//            _queue = q;
//            _syncEvents = e;
//        }
//        // Producer.ThreadRun  
//        public void ThreadRun()
//        {
//            int count = 0;
//            Random r = new Random();

//            while (!_syncEvents.ExitThreadEvent.WaitOne(0, false))
//            {
//                lock (((ICollection)_queue).SyncRoot)
//                {
//                    while (_queue.Count < 20)
//                    {
//                        _queue.Enqueue(r.Next(0, 100));
//                        _syncEvents.NewItemEvent.Set();
//                        count++;
//                    }
//                }
//            }
//            Console.WriteLine("Producer thread: produced {0} items", count);
//        }
//        private Queue<int> _queue;
//        private SyncEvents _syncEvents;
//    }


//    public class Consumer
//    {
//        public Consumer(Queue<int> q, SyncEvents e) 
//        {
//            _queue = q; 
//            _syncEvents = e;
//        }  
//        // Consumer.ThreadRun 
//        public void ThreadRun()
//        {
//            int count = 0;
//            while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1)
//            {
//                lock (((ICollection)_queue).SyncRoot)
//                {
//                    int item = _queue.Dequeue();
//                }
//                count++;
//            }
//            Console.WriteLine("Consumer Thread: consumed {0} items", count);
//        }
//        private Queue<int> _queue;
//        private SyncEvents _syncEvents;
//    }


//    public class ThreadSyncSample
//    {
//        private static void ShowQueueContents(Queue<int> q)
//        {
//            lock (((ICollection)q).SyncRoot)
//            {
//                foreach (int item in q)
//                {
//                    Console.Write("{0} ", item);
//                }
//            }
//            Console.WriteLine();
//        }
//        //static void Main()
//        //{
//        //    Queue<int> queue = new Queue<int>();
//        //    SyncEvents syncEvents = new SyncEvents();
//        //    Console.WriteLine("Configuring worker threads...");
//        //    Producer producer = new Producer(queue, syncEvents);
//        //    Consumer consumer = new Consumer(queue, syncEvents);
//        //    Thread producerThread = new Thread(producer.ThreadRun);
//        //    Thread consumerThread = new Thread(consumer.ThreadRun);
//        //    Console.WriteLine("Launching producer and consumer threads...");
//        //    producerThread.Start();
//        //    consumerThread.Start();
//        //    for (int i = 0; i < 4; i++)
//        //    {
//        //        Thread.Sleep(2500);
//        //        ShowQueueContents(queue);
//        //    } Console.WriteLine("Signaling threads to terminate...");
//        //    syncEvents.ExitThreadEvent.Set(); 
//        //    producerThread.Join();
//        //    consumerThread.Join();
//        //}
//    }
//}